package com.caixy.adminSystem.controller;

import com.caixy.adminSystem.common.BaseResponse;
import com.caixy.adminSystem.common.ErrorCode;
import com.caixy.adminSystem.common.ResultUtils;
import com.caixy.adminSystem.exception.BusinessException;
import com.caixy.adminSystem.model.dto.filecategory.AddFileCategoryRequest;
import com.caixy.adminSystem.model.dto.filecategory.DeleteFileCategoryRequest;
import com.caixy.adminSystem.model.dto.filecategory.SelectFileCategoryRequest;
import com.caixy.adminSystem.model.dto.filecategory.UpdateFileCategoryRequest;
import com.caixy.adminSystem.model.entity.FileCategory;
import com.caixy.adminSystem.model.entity.User;
import com.caixy.adminSystem.service.FileCategoryService;
import com.caixy.adminSystem.service.FileInfoService;
import com.caixy.adminSystem.service.FileService;
import com.caixy.adminSystem.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/FileCategory")
public class FileCategoryController {
    @Resource
    private UserService userService;

    @Resource
    private FileCategoryService fileCategoryService;
    @Resource
    private FileInfoService fileInfoService;

    @PostMapping("/add")
    public BaseResponse<String> AddFileCategoryComtroller(@RequestBody AddFileCategoryRequest addFileCategoryRequest, HttpServletRequest httpServletRequest){
        if (addFileCategoryRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
       User loginer= userService.getLoginUser(httpServletRequest);
       addFileCategoryRequest.setCreatorId(loginer.getId());
        FileCategory fileCategory = new FileCategory();
        BeanUtils.copyProperties(addFileCategoryRequest,fileCategory);
        fileCategoryService.check(fileCategory,true);
       fileCategoryService.save(fileCategory);
       return ResultUtils.success("");
    }
    @PostMapping("/delete")
    public BaseResponse<String> DeleteFileCategoryComtroller(@RequestBody DeleteFileCategoryRequest deleteFileCategoryRequest, HttpServletRequest httpServletRequest){
        if (deleteFileCategoryRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer= userService.getLoginUser(httpServletRequest);
        deleteFileCategoryRequest.setCreatorId(loginer.getId());
        FileCategory fileCategory = new FileCategory();
        BeanUtils.copyProperties(deleteFileCategoryRequest,fileCategory);
        if (fileInfoService.CountFileImfoByCategoryId(fileCategory.getId())>0){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }else {
            fileCategoryService.removeById(fileCategory.getId());
        }


        return ResultUtils.success("");
    }
    @PostMapping("/update")
    public BaseResponse<String> UpdateFileCategoryComtroller(@RequestBody UpdateFileCategoryRequest updateFileCategoryRequest, HttpServletRequest httpServletRequest){
        if (updateFileCategoryRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer= userService.getLoginUser(httpServletRequest);
        updateFileCategoryRequest.setCreatorId(loginer.getId());
        FileCategory fileCategory = new FileCategory();
        BeanUtils.copyProperties(updateFileCategoryRequest,fileCategory);
        fileCategoryService.check(fileCategory,true);
        fileCategoryService.updateById(fileCategory);
        return ResultUtils.success("");
    }
    @PostMapping("/select")
    public BaseResponse<String> SelectFileCategoryComtroller(@RequestBody SelectFileCategoryRequest selectFileCategoryRequest, HttpServletRequest httpServletRequest){
        if (selectFileCategoryRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer= userService.getLoginUser(httpServletRequest);
        selectFileCategoryRequest.setCreatorId(loginer.getId());
        FileCategory fileCategory = new FileCategory();
        BeanUtils.copyProperties(selectFileCategoryRequest,fileCategory);
        fileCategoryService.getById(fileCategory.getId());

        return ResultUtils.success("");
    }

}
